package kr.pfg.applications.captureyourscreen.display
{
	import flash.desktop.Clipboard;
	import flash.desktop.NativeDragManager;
	import flash.desktop.NativeDragOptions;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getDefinitionByName;
	
	import kr.pfg.applications.captureyourscreen.utils.CapturedImageSaver;

	public class FrameWindow extends Sprite
	{
		public var clickArea:Sprite;
		public var dragArea:Sprite;
		
		private var clipboard:Clipboard;
		
		public function FrameWindow()
		{
			super();
			
			this.dragArea.buttonMode = true;
			this.clickArea.buttonMode = true;
		}
		
		/**
		 * Capture image show user and temparary save a jpg file.
		 */
		
		public function setCaptureImage( bmd:BitmapData ):void
		{
			var copyBmd:BitmapData = bmd.clone();
			copyBmd.applyFilter( copyBmd, copyBmd.rect, new Point( 0, 0), new BlurFilter( 4, 4, 2 ) );
			copyBmd.colorTransform( copyBmd.rect, new ColorTransform( 0.8, 0.8, 0.8 ) );
			var bmp:Bitmap = new Bitmap( copyBmd );
			this.clickArea.addChild( bmp );
			this.showSmallImage();
			this.clipboard = CapturedImageSaver.getToSaveAsJPGClipboard( bmd, "image.jpg" );
			
		}
		
		/**
		 * Small image create and show.
		 */ 
		
		private var smallImage:Sprite;
		private function showSmallImage():void
		{
		 	if( !this.smallImage )
		 	{
		 		this.smallImage = new Sprite();
		 		this.stage.addChild( this.smallImage );
		 	}
		 	
		 	var dragIcon:Bitmap = new Bitmap( this.loadBmpAssets( "icon" ) );
		 	if( this.smallImage.numChildren == 0 )
		 	{
		 		this.smallImage.addChild( dragIcon );
		 		this.smallImage.x = this.width / 2 - this.smallImage.width / 2;
		 		this.smallImage.y = this.height / 2 - this.smallImage.height / 2;
		 		
		 		var dragTxt:TextField = new TextField();
		 		dragTxt.mouseEnabled = false;
		 		dragTxt.selectable = false;
		 		dragTxt.text = "Drag me out~";
		 		dragTxt.textColor = 0xFFCC32;
		 		dragTxt.filters = [ new GlowFilter( 0x333333, 1, 2, 2, 3, 2 ) ];
		 		dragTxt.x = this.smallImage.width / 2 - dragTxt.width / 2;
		 		dragTxt.y = -dragTxt.textHeight;
		 		this.smallImage.addChild( dragTxt );
		 		var tf:TextFormat = new TextFormat();
		 		tf.size = 16
		 		dragTxt.setTextFormat( tf );
		 	}
		 		
		 	this.smallImage.buttonMode = true;
			this.smallImage.addEventListener( MouseEvent.MOUSE_DOWN, this.dragSmallIamge );
		}
		
		/**
		 * Load library bitmap assets.
		 */ 
		 
		private function loadBmpAssets( name:String ):BitmapData
		{
	 		var referClass:Class = getDefinitionByName( name ) as Class;
			var bmd:BitmapData = new referClass( -1, -1 );
		 	
			return bmd;
		}
		
		/**
		 * When user drag a small image, save a jpg file at user's desktop. 
		 */ 
		
		private function dragSmallIamge( e:MouseEvent ):void
		{			
			var dragIcon:BitmapData = this.loadBmpAssets( "icon_add" );
			var options:NativeDragOptions = new NativeDragOptions();
			options.allowCopy = false;
			options.allowLink = false;
			
			NativeDragManager.doDrag( this.clickArea, this.clipboard, dragIcon, null, options );
		}
	}
}